package com.example.demo.service;

import com.example.demo.mapper.DepartmentMapper;
import com.example.demo.mapper.StaffMapper;
import com.example.demo.pojo.Staff;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class StaffService {
    @Autowired
    private DepartmentMapper departmentDao;
    @Autowired
    private StaffMapper staffDao;

    /**
     * 获取所有员工
     * @return 所有员工
     */
    public List<Staff> getAllTheStaffs(){
        List<Staff> staff = staffDao.queryStaffList();
        System.out.println(staff);
        return staff;
    }

    /**
     * 添加新员工
     * @param staff
     */
    @Transactional
    public void addNewStaff(Staff staff){
        staffDao.addStaff(staff);
        departmentDao.addEmployeeToDepartment(staff.getS_id(), staff.getDepartment().getId(), staff.getJob().getId());
    }

    /**
     * 根据id获取员工
     * @param id 员工id
     * @return 员工具体详细
     */
    public Staff getStaff(int id){
        return staffDao.queryStaffById(id);
    }

    /**
     * 更新员工数据
     * @param staff 新数据
     */
    public void updateStaff(Staff staff){
        System.out.println(staff);
        staffDao.updateStaff(staff);
        departmentDao.updateStaffDepartmentJob(staff.getS_id(),staff.getDepartment().getId(),staff.getJob().getId());
    }

    /**
     * 根据员工id删除员工
     * @param id 员工id
     */
    @Transactional
    public void deleteStaffById(int id){
        departmentDao.deleteStaffInDepartment(id);
        staffDao.deleteStaff(id);
    }

    public List<Staff> getStaffByCondition( String conditionText,
                                            String conditionType,
                                            String conditionGender,
                                            String conditionJob,
                                            String conditionDepartment){
        if("".equals(conditionText.trim())){
            conditionText = null;
        }
        switch (conditionType){
            case "姓名":
                conditionType = "s_name";
                break;
            case "手机号":
                conditionType = "phone";
                break;
            case "员工id":
                conditionType = "staff.s_id";
                break;
            default:
                conditionType = null;
        }
        if("任意".equals(conditionGender)){
            conditionGender = null;
        }
        if("任意".equals(conditionDepartment.trim())){
            conditionDepartment = null;
        }
        if("任意".equals(conditionJob.trim())){
            conditionJob = null;
        }
        return staffDao.getStaffByCondition(conditionText,conditionType,conditionGender,conditionJob,conditionDepartment);
    }
}
